package com.aitaoxue.palx.adapter;

import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import butterknife.ButterKnife;
import butterknife.InjectView;

import com.aitaoxue.palx.R;
import com.aitaoxue.palx.api.ApiHttpClient;
import com.aitaoxue.palx.base.ListBaseAdapter;
import com.aitaoxue.palx.bean.ErrorBook;
import com.aitaoxue.palx.ui.PhotosActivity;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;

public class ErrorBookAdapter extends ListBaseAdapter {

	private Context mContext;

	DisplayImageOptions options = null;

	public ErrorBookAdapter(Context context) {
		mContext = context;

		options = new DisplayImageOptions.Builder().cacheInMemory(false)
				.cacheOnDisk(true).build();
	}

	@Override
	protected View getRealView(final int position, View convertView,
			ViewGroup parent) {
		ViewHolder viewHolder = null;
		if (convertView == null || convertView.getTag() == null) {
			convertView = LayoutInflater.from(parent.getContext()).inflate(
					R.layout.list_cell_error_book, null);
			viewHolder = new ViewHolder(convertView);
			convertView.setTag(viewHolder);
		} else {
			viewHolder = (ViewHolder) convertView.getTag();
		}

		ErrorBook errorBook = (ErrorBook) _data.get(position);

		viewHolder.time.setText(errorBook.getPost_date());
		viewHolder.weekDay.setText(errorBook.getPost_date_week());
		viewHolder.course.setText(errorBook.getBook_name());
		viewHolder.desc.setText(errorBook.getPage_desc());

		// ImageLoader.getInstance().displayImage(
		// ApiHttpClient.ERROR_BOOK_PREFIX + errorBook.getPage_picture(),
		// viewHolder.img);

		ImageLoader.getInstance().displayImage(
				ApiHttpClient.ERROR_BOOK_PREFIX + errorBook.getPage_picture(),
				viewHolder.img, options);

		viewHolder.img.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent intent = new Intent(mContext, PhotosActivity.class);

				intent.putExtra("count", _data.size());
				intent.putExtra("index", position);

				List<ErrorBook> list = _data;
				String[] times = new String[list.size()];
				String[] descs = new String[list.size()];
				String[] imgs = new String[list.size()];
				for (int index = 0; index < list.size(); index++) {
					ErrorBook book = list.get(index);
					times[index] = book.getPost_date() + " "
							+ book.getPost_date_week();
					descs[index] = book.getPage_desc();
					imgs[index] = book.getPage_picture();
				}

				intent.putExtra("times", times);
				intent.putExtra("descs", descs);
				intent.putExtra("imgs", imgs);

				mContext.startActivity(intent);
			}
		});

		return convertView;
	}

	class ViewHolder {
		@InjectView(R.id.time)
		TextView time;
		@InjectView(R.id.weekday)
		TextView weekDay;
		@InjectView(R.id.course)
		TextView course;
		@InjectView(R.id.desc)
		TextView desc;
		@InjectView(R.id.thumbnail)
		ImageView img;

		public ViewHolder(View view) {
			ButterKnife.inject(this, view);
		}

	}

}
